package gek.uni.aspirant.uml.utils.wizzards;

import gek.uni.aspirant.uml.utils.Messages;
import gek.uni.aspirant.uml.utils.providers.OclContentProvider;
import gek.uni.aspirant.uml.utils.providers.OclLabelProvider;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.ValueSpecification;

public class SelectOclExpressionWizardPage extends WizardPage {
  private Label fileNameLabel;
  private Label lblFullSourceCode;
  private ListViewer oclElemsListViewer;
  private Button btnBrowse;
  private Label umlElementLabel;
  private String oclCodeString = "";

  /**
   * Create the wizard.
   */
  public SelectOclExpressionWizardPage() {
    super("wizardPage");
    setTitle("Messages.SELECT_OCL_EXPRESSION_PAGE_TITLE");
    setDescription("Messages.SELECT_OCL_EXPRESSION_PAGE_TITLE");
  }

  @Override
  public void setVisible(boolean visible) {
    super.setVisible(visible);
    if (visible) {
      Object element = ((SelectUmlContextElementWizardPage) getPreviousPage())
          .getUmlElement();
      oclElemsListViewer.setInput(element);
      if (element instanceof NamedElement)
        umlElementLabel.setText(((NamedElement) element).getName());
      else
        umlElementLabel.setText(element.toString());
    }
  }

  @Override
  public boolean isPageComplete() {
    return false == "".equals(oclCodeString);
  }

  public String getOclCodeString() {
    return oclCodeString;
  }

  /**
   * Create contents of the wizard.
   * 
   * @param parent
   */
  public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);

    setControl(container);
    container.setLayout(new GridLayout(1, false));

    Composite selectSourceComposite = createSelectSourceComposite(container,
        SWT.NONE);
    GridData gd_selectSourceComposite = new GridData(SWT.FILL, SWT.TOP, true,
        false, 1, 1);
    selectSourceComposite.setLayoutData(gd_selectSourceComposite);

    Composite selectElementComposite = createSelectElementComposite(container,
        SWT.NONE);
    GridData gd_selectElementComposite = new GridData(SWT.FILL, SWT.TOP, true,
        false, 1, 1);
    gd_selectElementComposite.heightHint = 87;
    selectElementComposite.setLayoutData(gd_selectElementComposite);
  }

  /**
   * Создает композайт с ListViewer'ом и полем для отображения текста
   * constraint'а
   * 
   * @param container
   * @param none
   * @return
   */
  private Composite createSelectElementComposite(Composite container, int none) {
    Composite selectElementComposite = new Composite(container, SWT.NONE);
    selectElementComposite.setLayout(new GridLayout(1, false));

    Label lblMessageselementslabel = new Label(selectElementComposite, SWT.NONE);
    lblMessageselementslabel.setText(Messages.ELEMENTS_LABEL);

    oclElemsListViewer = new ListViewer(selectElementComposite, SWT.BORDER
        | SWT.V_SCROLL);
    oclElemsListViewer.getControl().setLayoutData(
        new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    oclElemsListViewer.setContentProvider(new OclContentProvider());
    oclElemsListViewer.setLabelProvider(new OclLabelProvider());

    oclElemsListViewer
        .addSelectionChangedListener(new ISelectionChangedListener() {

          @Override
          public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            if (selection instanceof IStructuredSelection) {
              Object element = ((IStructuredSelection) selection)
                  .getFirstElement();
              if (element instanceof Constraint) {
                ValueSpecification specification = ((Constraint) element)
                    .getSpecification();
                oclCodeString = specification.stringValue();
                setPageComplete(true);
              } else {
                oclCodeString = "";
                setPageComplete(false);
              }

              lblFullSourceCode.setText(oclCodeString);
            }
          }
        });

    Composite composite = new Composite(container, SWT.NONE);
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    composite.setLayout(new GridLayout(1, false));

    Label lblFulltext = new Label(composite, SWT.NONE);
    lblFulltext.setText(Messages.FULL_TEXT_LABEL);

    lblFullSourceCode = new Label(composite, SWT.BORDER);
    lblFullSourceCode.setBackground(Display.getCurrent().getSystemColor(
        SWT.COLOR_WHITE));
    lblFullSourceCode.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
        true, 1, 1));
    return selectElementComposite;
  }

  private Composite createSelectSourceComposite(Composite container, int none) {

    Composite selectSourceComposite = new Composite(container, SWT.NONE);
    selectSourceComposite.setLayout(new GridLayout(3, false));

    Label lbl = new Label(selectSourceComposite, SWT.NONE);
    lbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1));
    lbl.setText(Messages.SOURCE_LABEL);

    Button fromModelRBtn = new Button(selectSourceComposite, SWT.RADIO);
    fromModelRBtn.setSelection(true);
    fromModelRBtn.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
        false, 1, 1));
    fromModelRBtn.setText(Messages.FROM_MODEL_LABEL);

    umlElementLabel = new Label(selectSourceComposite, SWT.BORDER);
    umlElementLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
        false, 2, 1));
    umlElementLabel.setBackground(Display.getCurrent().getSystemColor(
        SWT.COLOR_WHITE));
    umlElementLabel.setText("");

    final Button fromFileRBtn = new Button(selectSourceComposite, SWT.RADIO);
    fromFileRBtn.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        btnBrowse.setEnabled(fromFileRBtn.getSelection());
      }
    });
    fromFileRBtn.setText(Messages.FROM_FILE_LABEL);
    fromFileRBtn.setEnabled(false);

    fileNameLabel = new Label(selectSourceComposite, SWT.BORDER);
    fileNameLabel.setEnabled(false);
    fileNameLabel.setBackground(Display.getCurrent().getSystemColor(
        SWT.COLOR_WHITE));
    fileNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
        1, 1));

    btnBrowse = new Button(selectSourceComposite, SWT.NONE);
    btnBrowse.setEnabled(false);
    btnBrowse.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
        1, 1));
    btnBrowse.setText("Messages.BROWSE_BUTTON_TEXT");
    return selectSourceComposite;
  }
}
